package com.yookey.common.utils;

import cn.hutool.core.date.DateUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * className DateTimeUtil
 *
 * @Author: Zhang Shao Hui
 * @Description:
 * @Date:Create：in 2019/12/16 17:14
 * @Modified By：
 */
public class DateTimeUtil {
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT2 = "yyyy-MM-dd";
    private static final String DATE_FORMAT3 = "HH:mm";
    private static final String MILLS_DATE_FORMAT = "yyyyMMddHHmmssSSS";
    private static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static Date getYmdHms() {
        return DateUtil.parse(DateUtil.format(new Date(), DATE_FORMAT));
    }

    public static String getMillsYmdHms() {
        return DateUtil.format(new Date(), MILLS_DATE_FORMAT);
    }

    public static Date getEndOfDay(Date date) {
        return DateUtil.endOfDay(date);
    }

    public static Date getBeginOfDay(Date date) {
        return DateUtil.beginOfDay(date);
    }

    public static String getYmdHmsFormat(Date date) {
        return DateUtil.format(date, DATE_FORMAT);
    }

    public static String getYmdHmsFormatDate(Date date) {
        return DateUtil.format(date, DATE_FORMAT2);
    }
    public static String getYmdHmsFormatTime(Date date) {
        return DateUtil.format(date, DATE_FORMAT3);
    }

    public static Date getYmdHms4Str(String dateStr) {
        return parseDate(dateStr);
    }

    /**
     * string日期转换为日期对象
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        if (date == null) return null;
        try {
            return SIMPLE_DATE_FORMAT.parse(date.trim());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前时间增加几小时后的时间
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addHours(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, amount);
        return c.getTime();
    }

    /**
     * 当时时间是否在开始时间和结束时间中间
     *
     * @param start
     * @param end
     * @return
     */
    public static boolean curTimeBetween(Date start, Date end) {
        Date curDate = getCurrentDate();
        if (after(curDate, start) && after(end, curDate)) {
            return true;
        }

        return false;
    }

    /**
     * 获取当期日期
     *
     * @return
     */
    public static Date getCurrentDate() {
        return new Date();
    }

    /**
     * 判断d1是否大于d2
     *
     * @param d1
     * @param d2
     * @return
     */
    public static boolean after(Date d1, Date d2) {
        if (d1 != null && d2 != null) {
            return d1.after(d2);
        }
        if (d1 != null && d2 == null) {
            return true;
        }
        return false;
    }

    /**
     * 获取当前日期增加几天后的日期
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addDays(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, amount);
        return c.getTime();
    }

    /**
     *获取下一天的开始时间
     * @return
     */
    public static Date getNextDay(Date nowDate) {
        Date date = DateUtil.beginOfDay(nowDate);
        return addDays(date, 1);
    }

    /**
     *获取下一月的开始时间
     * @return
     */
    public static Date getNextMonth(Date nowDate) {
        Date date = DateUtil.beginOfMonth(nowDate);
        return addMonth(date, 1);
    }

    /**
     *获取下一年的开始时间
     * @return
     */
    public static Date getNextYear(Date nowDate) {
        Date date = DateUtil.beginOfYear(nowDate);
        return addYear(date, 1);
    }

    /**
     * 月份加
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addMonth(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, amount);
        return c.getTime();
    }

    /**
     * 年份加
     *
     * @param date
     * @param amount
     * @return
     */
    public static Date addYear(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, amount);
        return c.getTime();
    }

    /**
     * 秒加
     * @param date
     * @param amount
     * @return
     */
    public static Date addSecond(Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, amount);
        return calendar.getTime();
    }
}
